import 'package:flutter/material.dart';

class ClipDemo extends StatefulWidget {
  const ClipDemo({Key? key}) : super(key: key);

  @override
  State<ClipDemo> createState() => _ClipDemoState();
}

class _ClipDemoState extends State<ClipDemo> {
  
  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    return Scaffold(
      appBar: AppBar(
        title: Text("圆形裁剪ClipOval组件的使用"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              width: width,
              height: 200,
              alignment: Alignment.center,
              color: Colors.grey,
              // 用来将ziwedget 裁剪成椭圆圆形
              child: ClipOval(
                // 用来定义裁剪路径
                // Clip.hardEdge 裁剪 不抗锯齿
                // Clip.antiAlias 抗锯齿
                clipBehavior: Clip.antiAliasWithSaveLayer,
                child: Container(
                  color: Colors.blue,
                  width: 200,
                  height: 200,
                ),
              ),
            ),
            SizedBox(height: 20),
            Container(
              width: width,
              height: 300,
              alignment: Alignment.center,
              // 默认裁剪圆形
              child: ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(30)),
                clipBehavior: Clip.antiAliasWithSaveLayer,
                child: Container(
                  color:Colors.blue,
                  width: 200,
                  height: 200,
                ),
              ),
            ),
             Container(
              width: width,
              height: 300,
              alignment: Alignment.center,
              // 默认裁剪圆形
              child: ClipPath(
                clipper: CustomPathClip(),
                clipBehavior: Clip.antiAliasWithSaveLayer,
                child: Container(
                  color:Colors.blue,
                  width: 200,
                  height: 200,
                ),
              ),
            )
          
          ],
        ),
      ),
    );  
  }
}

class CustomPathClip extends CustomClipper<Path>{
  @override
  Path getClip(Size size) {
    // TODO: implement getClip
    Path path = new Path();
    // 起点移动到终点
    path.moveTo(100, 0);
    path.lineTo(150, 100);
    path.lineTo(50, 100);
    path.close();
    return path;
  }
  
  @override
  bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
    // TODO: implement shouldReclip
    throw UnimplementedError();
  }  
}